/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.time;

/** 
 * Geocentric Coordinate Time.  Coordinate time at the center of mass
 * of the Earth.  This time scale depends linearly from {@link TTScale
 * Terrestrial Time}.
 * 
 * This class is intended to be accessed via to the {@link
 * TimeScalesFactory} class.
 * 
 * @author Luc Maisonobe
 * @see AbsoluteDate
 */
public final class TCGScale 
    extends Object
    implements TimeScale
{
    public final static TimeScale Instance = new TCGScale();


    /** 
     * LG rate.
     */
    private static double LG_RATE = 6.969290134e-10;

    /** 
     * Reference date for TCG.
     * 
     * The reference date is such that the three following instants are equal:
     * <ul>
     *   <li>1977-01-01T00:00:32.184 TT</li>
     *   <li>1977-01-01T00:00:32.184 TCG</li>
     *   <li>1977-01-01T00:00:00.000 TAI</li>
     * </ul>
     */
    private static final AbsoluteDate REFERENCE_DATE =
        new AbsoluteDate(1977, 01, 01, TimeScalesFactory.getTAI());
    /** 
     * Offset between TT and TAI scales.
     */
    private static final double TT_OFFSET =
        TimeScalesFactory.getTT().offsetFromTAI(REFERENCE_DATE);


    private TCGScale() {
        super();
    }


    public double offsetFromTAI(AbsoluteDate date) {
        return (TT_OFFSET + (LG_RATE * date.durationFrom(REFERENCE_DATE)));
    }
    public double offsetToTAI(DateComponents date, TimeComponents time) {
        AbsoluteDate reference = new AbsoluteDate(date, time, TimeScalesFactory.getTAI());
        double offset = 0;
        for (int i = 0; i < 3; i++) {
            offset = -this.offsetFromTAI(new AbsoluteDate(reference, offset));
        }
        return offset;
    }
    public String getName() {
        return "TCG";
    }
    public String toString() {
        return this.getName();
    }
}
